import pygame
from pygame.locals import *
from random import *
class Fa_enemy(pygame.sprite.Sprite):
    SMALL_E_B = 4
    MID_E_B = 14
    LAG_E_B = 30
    BOSS_E_B = 500

    def __init__(self,enemy_image1,enemy_image2,bg_size):
        super(Fa_enemy, self).__init__()
        self.bg_width,self.bg_height = bg_size
        self.image1 = pygame.image.load(enemy_image1).convert_alpha()
        self.image2 = pygame.image.load(enemy_image2).convert_alpha()
        self.rect = self.image1.get_rect()
        self.mask = pygame.mask.from_surface(self.image1)
        self.is_hit = False
        self.die_index = 0
        self.max_blood = 1
        self.speed_offset = 0
        self.reset()
        self.is_skill = False
        self.active = True


    def disappear_move(self):
            self.rect.top += self.speed[0]

    def move(self):
        if self.rect.top > self.bg_height:
            self.reset()
        else:
            self.rect.top +=self.speed[0]


        if 0 - self.rect.width < self.rect.left < self.bg_width:
            self.rect.left +=self.speed[1]
        else:
            self.reset()

    def reset(self):
        self.rect.left,self.rect.bottom =\
            randint(0,self.bg_width - self.rect.width),\
            randint(-4*self.bg_height , 0)
        self.die_index = 0
        self.active = True


class Small_enemy(Fa_enemy):
    def __init__(self,bg_size):
        Fa_enemy.__init__(self,"images/enemy1.png","images/enemy1.png",bg_size)
        self.image_hited = pygame.image.load("images/enemy1_hit.png")
        self.die_image = []
        self.die_image.append(pygame.image.load("images/enemy1_down1.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/enemy1_down2.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/enemy1_down3.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/enemy1_down4.png").convert_alpha())
        self.score = 100
        self.max_blood = Fa_enemy.SMALL_E_B
        self.blood = self.max_blood
    def reset(self):
        Fa_enemy.reset(self)
        self.blood = self.max_blood
        self.speed = randint(2 + self.speed_offset,3+ self.speed_offset),randint(-3- self.speed_offset,3 + + self.speed_offset)
        self.active = True

class mid_enemy(Fa_enemy):
    def __init__(self,bg_size):
        Fa_enemy.__init__(self,"images/enemy2.png","images/enemy2.png",bg_size)
        self.image_hited = pygame.image.load("images/enemy2_hit.png")
        self.die_image = []
        self.die_image.append(pygame.image.load("images/enemy2_down1.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/enemy2_down2.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/enemy2_down3.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/enemy2_down4.png").convert_alpha())
        self.score = 200
        self.max_blood = Fa_enemy.MID_E_B
        self.blood = self.max_blood

    def reset(self):
        Fa_enemy.reset(self)
        self.blood = self.max_blood
        self.speed = randint(1 + self.speed_offset, 2+ self.speed_offset), randint(-self.speed_offset-1 ,  self.speed_offset+1)
        self.active = True


class large_enemy(Fa_enemy):
    def __init__(self,bg_size):
        Fa_enemy.__init__(self,"images/enemy3_n1.png","images/enemy3_n2.png",bg_size)
        self.image_hited = pygame.image.load("images/enemy3_hit.png")
        self.die_image = []
        self.die_image.append(pygame.image.load("images/enemy3_down1.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/enemy3_down2.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/enemy3_down3.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/enemy3_down4.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/enemy3_down5.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/enemy3_down6.png").convert_alpha())
        self.score = 500
        self.max_blood = Fa_enemy.LAG_E_B
        self.blood = self.max_blood

    def reset(self):
        Fa_enemy.reset(self)
        self.blood = self.max_blood
        self.speed = randint(1 + self.speed_offset, 1+ self.speed_offset),  randint( -self.speed_offset , self.speed_offset)
        self.active = True

class boss(Fa_enemy):
    def __init__(self, bg_size,level):
        Fa_enemy.__init__(self, "images/boss1.png", "images/boss2.png", bg_size)
        self.image_hited = pygame.image.load("images/boss1.png")
        self.skill1 = []
        self.skill1.append(pygame.image.load("images/boss_skill1.png").convert_alpha())
        self.skill1.append(pygame.image.load("images/boss_skill2.png").convert_alpha())
        self.skill1.append(pygame.image.load("images/boss_skill1.png").convert_alpha())

        self.skill2 = []
        self.skill2.append(pygame.image.load("images/boss_angey1.png").convert_alpha())
        self.skill2.append(pygame.image.load("images/boss_angey1.png").convert_alpha())
        self.skill2.append(pygame.image.load("images/boss_angey2.png").convert_alpha())
        self.skill2.append(pygame.image.load("images/boss_angey3.png").convert_alpha())
        self.skill2.append(pygame.image.load("images/boss_angey3.png").convert_alpha())
        self.skill2.append(pygame.image.load("images/boss_angey3.png").convert_alpha())

        self.level = level
        self.die_image = []
        self.die_image.append(pygame.image.load("images/boss_d_1.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/boss_d_2.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/boss_d_3.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/boss_d_4.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/boss_d_5.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/boss_d_6.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/boss_d_7.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/boss_d_8.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/boss_d_9.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/boss_d_5.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/boss_d_6.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/boss_d_7.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/boss_d_8.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/boss_d_9.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/boss_d_5.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/boss_d_6.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/boss_d_7.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/boss_d_8.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/boss_d_9.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/boss_d_5.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/boss_d_6.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/boss_d_7.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/boss_d_8.png").convert_alpha())
        self.die_image.append(pygame.image.load("images/boss_d_9.png").convert_alpha())

        self.max_blood = Fa_enemy.BOSS_E_B * ( 1 +  self.level)
        #self.max_blood = 1
        self.blood = self.max_blood
        self.skill_index = 0
        self.is_skill = False
        self.skill_intrival = 420 / ( 1 + 0.5 * self.level)
        self.speed1 = 1 + level
        self.bullet_num = 20
        self.score = 0
        self.low_limit = 0.2 + 0.08 * level
        self.is_skill2 = False
        self.in_skill2 = False
        self.ball_num = 10

    def move(self):
        if self.rect.left + self.speed1 <= 0 or self.rect.right + self.speed1 >= self.bg_width:
            self.speed1 = -self.speed1
        else:
            self.rect.left += self.speed1
        # if (self.rect.left + self.rect.right)//2 - self.speed1 > position[0]:
        #     self.rect.left -=self.speed1
        # elif (self.rect.left + self.rect.right)//2 + self.speed1 + position[0] :
        #     self.rect.left  +=self.speed1
        # else:
        #     self.rect.left = position[0] - (self.rect.right - self.rect.left)


    def reset(self):
        self.rect.left, self.rect.top = \
            (self.bg_width - self.rect.width)//2 , 50
        self.die_index = 0
        self.speed = randint(1, 1), 0
        self.active = True